package com.bweab.controll.dormitorytbcontrol;

import com.bweab.pojo.DormitoryTb;
import com.bweab.service.api.DormitoryTbServiceInterface;
import com.bweab.service.api.UsersTbServiceInterface;
import com.bweab.service.impl.DormitoryTbServiceInterfaceImpl;
import com.bweab.service.impl.UsersTbServiceInterfaceImpl;
import com.bweab.util.JSONResult;
import com.bweab.util.JSONStats;
import com.bweab.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/checkDormitoryName")
public class CheckDormitoryName extends HttpServlet {

    //创建寝室服务对象

    DormitoryTbServiceInterface dormitoryTbService=new DormitoryTbServiceInterfaceImpl();
    //创建返回对象
    JSONResult jsonResult = new JSONResult();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求
        String dormitoryName = req.getParameter("dormitorynname");
        System.out.println(dormitoryName);
        if(dormitoryName!=null){
            //调用服务层
            DormitoryTb  dormitoryInfo= dormitoryTbService.getDormitoryInfo(dormitoryName);
            System.out.println(dormitoryInfo);

            //设置响应状态码
            if(dormitoryInfo!=null){
                jsonResult.setStatusCode(JSONStats.CHECK_DORMITORY_NAME_OK.getStatusCode());
                jsonResult.setMessage(JSONStats.CHECK_DORMITORY_NAME_OK.getMessage());
                //设置响应体数据
                jsonResult.setData(dormitoryInfo);

            }else {
                //失败的情况下
                jsonResult.setStatusCode(JSONStats.CHECK_DORMITORY_NAME_ERROR.getStatusCode());
                jsonResult.setMessage(JSONStats.CHECK_DORMITORY_NAME_ERROR.getMessage());
            }
        }else {
            //失败的情况下
            jsonResult.setStatusCode(JSONStats.CHECK_DORMITORY_NAME_ERROR.getStatusCode());
            jsonResult.setMessage(JSONStats.CHECK_DORMITORY_NAME_ERROR.getMessage());
        }

        //向客户端发送信息
        WebUtil.writeJson(resp,jsonResult);
    }
}
